package com.ruoyi.tt.service.impl;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.tt.domain.dto.ArticleSearchDto;
import com.ruoyi.tt.domain.vo.ArticleCommentVO;
import com.ruoyi.tt.domain.vo.ArticleDetailVO;
import com.ruoyi.tt.domain.vo.ArticleVO;
import com.ruoyi.tt.mapper.TtArticleCommentMapper;
import com.ruoyi.tt.mapper.TtCollectionMapper;
import com.ruoyi.tt.service.ThreadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tt.mapper.TtArticleMapper;
import com.ruoyi.tt.domain.TtArticle;
import com.ruoyi.tt.service.ITtArticleService;

/**
 * 文章管理Service业务层处理
 *
 * @author 梁静茹
 * @date 2022-10-17
 */
@Service
public class TtArticleServiceImpl implements ITtArticleService {
    @Autowired
    private TtArticleMapper ttArticleMapper;

    @Autowired
    private TtArticleCommentMapper ttArticleCommentMapper;

    @Autowired
    private TtCollectionMapper collectionMapper;

    @Autowired
    private ThreadService threadService;
    private static ExecutorService threadPool = Executors.newFixedThreadPool(20);

    /**
     * 查询文章管理
     *
     * @param id 文章管理主键
     * @return 文章管理
     */
    @Override
    public TtArticle selectTtArticleById(Long id) {
        return ttArticleMapper.selectTtArticleById(id);
    }

    /**
     * 查询文章管理列表
     *
     * @param ttArticle 文章管理
     * @return 文章管理
     */
    @Override
    public List<TtArticle> selectTtArticleList(TtArticle ttArticle) {
        // TODO 分类昵称显示
        return ttArticleMapper.selectTtArticleList(ttArticle);
    }

    /**
     * 新增文章管理
     *
     * @param ttArticle 文章管理
     * @return 结果
     */
    @Override
    public int insertTtArticle(TtArticle ttArticle) {
        ttArticle.setCreateTime(DateUtils.getNowDate());
        Long userId = SecurityUtils.getUserId();
        ttArticle.setUserId(userId);
        return ttArticleMapper.insertTtArticle(ttArticle);
    }

    /**
     * 修改文章管理
     *
     * @param ttArticle 文章管理
     * @return 结果
     */
    @Override
    public int updateTtArticle(TtArticle ttArticle) {
        return ttArticleMapper.updateTtArticle(ttArticle);
    }

    /**
     * 批量删除文章管理
     *
     * @param ids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteTtArticleByIds(Long[] ids) {
        return ttArticleMapper.deleteTtArticleByIds(ids);
    }

    /**
     * 删除文章管理信息
     *
     * @param id 文章管理主键
     * @return 结果
     */
    @Override
    public int deleteTtArticleById(Long id) {
        return ttArticleMapper.deleteTtArticleById(id);
    }

    /**
     * 根据分类id获取文章列表
     *
     * @param typeId
     * @return
     */
    @Override
    public List<ArticleVO> getListByTypeId(Integer typeId, String time) {
        return ttArticleMapper.getListByTypeId(typeId, time).stream().map(articleVO -> {
            Integer id = articleVO.getId();
            Integer count = ttArticleCommentMapper.getArticleCommentCountByArticleId(id);
            articleVO.setCount(count == null ? 0 : count);
            return articleVO;
        }).collect(Collectors.toList());
    }

    /**
     * * 根据文章id，获取文章详情&评论列表
     *
     * @param articleId
     * @return
     */
    @Override
    public ArticleDetailVO getArticleDetailByArticleId(Integer articleId) {
        Long userId = SecurityUtils.getUserId();
        // 查看此用户是否拥有此收藏记录
        int i = collectionMapper.getCollectionByUserIdWithArticleId(articleId, userId);

        // 查询文章详情信息
        ArticleDetailVO articleDetailVO = ttArticleMapper.getArticleDetailByArticleId(articleId);
        Integer count = articleDetailVO.getCount(); // 浏览量
        TtArticle article = new TtArticle();
        article.setId(articleDetailVO.getId() + 0L);
        article.setCount(count + 1); // 自增1
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                ttArticleMapper.updateTtArticle(article);

            }
        });

//        threadService.updateViewCount(ttArticleMapper, article);
        List<ArticleCommentVO> list = ttArticleCommentMapper.getArticleCommentByArticleId(articleId);
        articleDetailVO.setArticleCommentVOList(list);
        if (i > 0) {
            articleDetailVO.setCollectionFlag(true);
        } else {
            articleDetailVO.setCollectionFlag(false);
        }
        return articleDetailVO;
    }

    /**
     * 根据文章标题关键字进行检索
     *
     * @param articleSearchDto
     * @return
     */
    @Override
    public List<ArticleVO> getArticleByKeyWord(ArticleSearchDto articleSearchDto) {
        System.out.println("articleSearchDto = " + articleSearchDto);
        List<ArticleVO> list = ttArticleMapper.getArticleByKeyWords(articleSearchDto.getKeyWord(), articleSearchDto.getTimestamp());
        list = list.stream().map(articleVO -> {
            Integer id = articleVO.getId();
            Integer count = ttArticleCommentMapper.getArticleCommentCountByArticleId(id);
            articleVO.setCount(count == null ? 0 : count);
            String title = articleVO.getTitle();
//            title.replace
            String replace = title.replaceAll(articleSearchDto.getKeyWord(), "<span style=\"color: #FF6600;\">" + articleSearchDto.getKeyWord() + "</span>");
            articleVO.setTitle(replace);
            return articleVO;
        }).collect(Collectors.toList());
        return list;
    }


}
